<?php

namespace app\admin\controller;

use think\Db;
use think\Exception;

Class Card extends Base
{
    public function list(){
        $obj = Db::table("tb_card_info")->field("info.*,agent.nickname as agent,agent2.nickname as agent2,shop.nickname as shop")->alias("info")->order('id asc');
        $param = $this->request->get();
        if (isset($param['word']) && $param['word']) {
            $obj->where('info.card', "like","%".$param['word']."%");
        }
        if (isset($param['start']) && $param['start']) {
            $obj->where('info.time', '>', strtotime($param['start'] . " 00:00:00"));
        }
        if (isset($param['end']) && $param['end']) {
            $obj->where('info.time', '<', strtotime($param['end'] . " 00:00:00"));
        }
        if (isset($param['status']) && $param['status']>-1) {
            $obj->where('info.status', $param['status']);
        }
        if (isset($param['agent_id']) && $param['agent_id']>0) {
            $obj->where('info.agent_id', $param['agent_id']);
        }
        if (isset($param['agent2_id']) && $param['agent2_id']>0) {
            $obj->where('info.agent2_id', $param['agent2_id']);
        }
        if (isset($param['shop_id']) && $param['shop_id']>0) {
            $obj->where('info.shop_id', $param['shop_id']);
        }
        $obj->join('tb_card_shop agent',"info.agent_id=agent.id","left");
        $obj->join('tb_card_shop shop',"info.shop_id=shop.id","left");
        $obj->join('tb_card_shop agent2',"info.agent2_id=agent2.id","left");
        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);
//        echo  Db::table("tb_card_shop")->getLastSql();
        $agents =  Db::table("tb_card_shop")->where("type",1)->select();
        $agents2 =  Db::table("tb_card_shop")->where("type",2)->select();
        $shops =  Db::table("tb_card_shop")->where("type",3)->select();
        return view('', ['list' => $list,'agents' => $agents,'shops' => $shops,'agents2' => $agents2]);
    }

    public function edit(){
        $id = $this->request->param('id');
        $row = Db::table("tb_card_info")->where("id",$id)->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $sqldata=[];
            $sqldata["shop_id"]=$data["shop_id"];
            $sqldata["agent_id"]=$data["agent_id"];
            $sqldata["agent2_id"]=$data["agent2_id"];
            if ($row["shop_id"]!=$sqldata["shop_id"]){
                $sqldata["shop_time"]=$data["shop_time"];
            }
            if ($row["agent_id"]!=$sqldata["agent_id"]){
                $sqldata["agent_time"]=$data["agent_time"];
            }
            if ($row["agent2_id"]!=$sqldata["agent2_id"]){
                $sqldata["agent2_time"]=$data["agent2_time"];
            }
            $sqldata["status"]=$data["status"];
            $res = Db::table("tb_card_info")->where("id",$id)->update($sqldata);
            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                //return ['status' => 'n', 'info' => '操作失败：'.Db::table("tb_card_info")->getLastSql()];
                return ['status' => 'n', 'info' => '数据未修改'];
            }
        }
        return view('', ['data' => $row]);
    }

    public function agent(){
        $obj = Db::table("tb_card_shop")->field("shop.*,partner.name as p_name")->alias("shop")->where("type",1)->order('id', 'desc');
        $param = $this->request->get();
        if (isset($param['word']) && $param['word']) {
            $obj->where('id|username', "like","%".$param['word']."%");
        }
        if (isset($param['start']) && $param['start']) {
            $obj->where('shop.time', '>', strtotime($param['start'] . " 00:00:00"));
        }
        if (isset($param['end']) && $param['end']) {
            $obj->where('shop.time', '<', strtotime($param['end'] . " 00:00:00"));
        }
        if (isset($param['partner_id']) && $param['partner_id']>0) {
            $obj->where('partner_id',$param['partner_id']);
        }
        $obj->join('tb_card_partner partner',"shop.partner_id=partner.id","left");

        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);
        $ps=Db::table("tb_card_partner")->order('id', 'desc')->select();
        return view('', ['list' => $list,'ps' => $ps]);
    }

    public function agentEdit(){
        $id = $this->request->param('id');
        $row = Db::table("tb_card_shop")->where("id",$id)->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (empty($data["partner_id"])){
                return ['status' => 'n', 'info' => '合作伙伴不能为空'];
            }
            $sqldata = array();
            $sqldata["username"]=$data["username"];
            $sqldata["nickname"]=$data["nickname"];
            $sqldata["partner_id"]=$data["partner_id"];
            $sqldata["type"]=1;
            $sqldata["time"]=time();
            $sqldata["bonus"]=$data["bonus"];
            if ($data["bonus"]>=1){
                return ['status' => 'n', 'info' => '分红比例必须小于1'];
            }
            if (empty($id)){
                if (empty($data["pass"])){
                    return ['status' => 'n', 'info' => '请输入密码'];
                }
                $sqldata["pass"]=md5($data["pass"]);
                $res = Db::table("tb_card_shop")->insert($sqldata);
            }else{
                if (!empty($data["pass"])){
                    $sqldata["pass"]=md5($data["pass"]);
                }
                $res = Db::table("tb_card_shop")->where("id",$id)->update($sqldata);
            }
            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败'];
            }
        }
        $partners=Db::table("tb_card_partner")->order('id', 'desc')->select();
        return view('', ['row' => $row,'partners' => $partners]);
    }

    public function agent2(){
        $obj = Db::table("tb_card_shop")->field("shop.*,partner.nickname as p_name")->alias("shop")->where("shop.type",2)->order('id', 'desc');
        $param = $this->request->get();
        if (isset($param['word']) && $param['word']) {
            $obj->where('id|username', "like","%".$param['word']."%");
        }
        if (isset($param['start']) && $param['start']) {
            $obj->where('shop.time', '>', strtotime($param['start'] . " 00:00:00"));
        }
        if (isset($param['end']) && $param['end']) {
            $obj->where('shop.time', '<', strtotime($param['end'] . " 00:00:00"));
        }
        if (isset($param['parent']) && $param['parent']>0) {
            $obj->where('parent',$param['parent']);
        }
        $obj->join('tb_card_shop partner',"shop.parent=partner.id","left");

        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);
        $ps=Db::table("tb_card_shop")->where("type",1)->select();
        return view('', ['list' => $list,'ps' => $ps]);
    }

    public function agent2Edit(){
        $id = $this->request->param('id');
        $row = Db::table("tb_card_shop")->where("id",$id)->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (empty($data["parent"])){
                return ['status' => 'n', 'info' => '代理商不能为空'];
            }
            $sqldata = array();
            $sqldata["username"]=$data["username"];
            $sqldata["nickname"]=$data["nickname"];
            $sqldata["parent"]=$data["parent"];
            $sqldata["type"]=2;
            $sqldata["time"]=time();
            $sqldata["bonus"]=$data["bonus"];
            if ($data["bonus"]>=1){
                return ['status' => 'n', 'info' => '分红比例必须小于1'];
            }
            if (empty($id)){
                if (empty($data["pass"])){
                    return ['status' => 'n', 'info' => '请输入密码'];
                }
                $sqldata["pass"]=md5($data["pass"]);
                $res = Db::table("tb_card_shop")->insert($sqldata);
            }else{
                if (!empty($data["pass"])){
                    $sqldata["pass"]=md5($data["pass"]);
                }
                $res = Db::table("tb_card_shop")->where("id",$id)->update($sqldata);
            }
            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败'];
            }
        }
        $partners=Db::table("tb_card_shop")->where("type",1)->order('id', 'desc')->select();
        return view('', ['row' => $row,'partners' => $partners]);
    }

    public function shop(){
        $obj = Db::table("tb_card_shop")->field("shop.*,partner.nickname as p_name")->alias("shop")->where("shop.type",3)->order('id', 'desc');
        $param = $this->request->get();
        if (isset($param['word']) && $param['word']) {
            $obj->where('id|username', "like","%".$param['word']."%");
        }
        if (isset($param['start']) && $param['start']) {
            $obj->where('shop.time', '>', strtotime($param['start'] . " 00:00:00"));
        }
        if (isset($param['end']) && $param['end']) {
            $obj->where('shop.time', '<', strtotime($param['end'] . " 00:00:00"));
        }
        if (isset($param['parent']) && $param['parent']>0) {
            $obj->where('parent',$param['parent']);
        }
        $obj->join('tb_card_shop partner',"shop.parent=partner.id","left");

        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);
        $ps=Db::table("tb_card_shop")->where("type",2)->select();
        return view('', ['list' => $list,'ps' => $ps]);
    }

    public function shopEdit(){
        $id = $this->request->param('id');
        $row = Db::table("tb_card_shop")->where("id",$id)->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (empty($data["parent"])){
                return ['status' => 'n', 'info' => '代理商不能为空'];
            }
            $sqldata = array();
            $sqldata["username"]=$data["username"];
            $sqldata["nickname"]=$data["nickname"];
            $sqldata["parent"]=$data["parent"];
            $sqldata["type"]=3;
            $sqldata["time"]=time();
            $sqldata["bonus"]=$data["bonus"];
            if ($data["bonus"]>=1){
                return ['status' => 'n', 'info' => '分红比例必须小于1'];
            }
            if (empty($id)){
                if (empty($data["pass"])){
                    return ['status' => 'n', 'info' => '请输入密码'];
                }
                $sqldata["pass"]=md5($data["pass"]);
                $res = Db::table("tb_card_shop")->insert($sqldata);
            }else{
                if (!empty($data["pass"])){
                    $sqldata["pass"]=md5($data["pass"]);
                }
                $res = Db::table("tb_card_shop")->where("id",$id)->update($sqldata);
            }
            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败'];
            }
        }
        $partners=Db::table("tb_card_shop")->where("type",2)->order('id', 'desc')->select();
        return view('', ['row' => $row,'partners' => $partners]);
    }

    public function partner(){
        $obj = Db::table("tb_card_partner")->order('id', 'desc');
        $param = $this->request->get();
        if (isset($param['word']) && $param['word']) {
            $obj->where('id|name', "like","%".$param['word']."%");
        }
        if (isset($param['start']) && $param['start']) {
            $obj->where('time', '>', strtotime($param['start'] . " 00:00:00"));
        }
        if (isset($param['end']) && $param['end']) {
            $obj->where('time', '<', strtotime($param['end'] . " 00:00:00"));
        }
        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);
        return view('', ['list' => $list]);
    }

    public function partnerEdit(){
        $id = $this->request->param('id');
        $row = Db::table("tb_card_partner")->where("id",$id)->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $sqldata = array();
            $sqldata["name"]=$data["name"];
            $sqldata["web"]=$data["web"];
            $sqldata["url"]=$data["url"];
            $sqldata["time"]=time();
            if (empty($id)){
                $res = Db::table("tb_card_partner")->insert($sqldata);
            }else{
                $res = Db::table("tb_card_partner")->where("id",$id)->update($sqldata);
            }
            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败'];
            }
        }
        return view('', ['row' => $row]);
    }


    public function agentCard(){
        $id = $this->request->param('id');
        $obj = Db::table("tb_card_shop")->where("type",1)->where('id', $id)->find();
        $row = Db::table("tb_card_info")->where("status",0)->where("agent_id is null")->order("id asc")->find();
        if ($this->request->isPost()) {
            if (!$obj){
                return ['status' => 'n', 'info' => '代理商不存在'];
            }
            if (!$obj["bonus"]){
                return ['status' => 'n', 'info' => '请设置派卡分红比例'];
            }
            $data = $this->request->post();
            $card = intval($data["card"]);
            $end = intval($data["end"]);
            if ($card<1000000000){
                return ['status' => 'n', 'info' => '开始卡号错误'];
            }
            if ($card>9999999999){
                return ['status' => 'n', 'info' => '开始卡号错误'];
            }
            if ($end<1000000000){
                return ['status' => 'n', 'info' => '结束卡号错误'];
            }
            if ($end>9999999999){
                return ['status' => 'n', 'info' => '结束卡号错误'];
            }

            $sqldata=[];
            $sqldata["agent_time"]=time();
            $sqldata["agent_id"]=$id;
            $res = Db::table("tb_card_info")->where("status",0)->where("agent_id is null")
                ->where("card",">=",$data["card"])->where("card","<=",$end)->update($sqldata);
            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败'];
            }
        }
        return view('', ['row' => $row,'data' => $obj]);
    }

    public function agent2Card(){
        $id = $this->request->param('id');
        $obj = Db::table("tb_card_shop")->where("type",2)->where('id', $id)->find();
        $row = Db::table("tb_card_info")->where("status",0)->where("agent_id",$obj["parent"])->where("agent2_id is null")->order("id asc")->find();
        if ($this->request->isPost()) {
            if (!$obj){
                return ['status' => 'n', 'info' => '商户不存在'];
            }
            if (!$obj["bonus"]){
                return ['status' => 'n', 'info' => '请设置派卡分红比例'];
            }
            $data = $this->request->post();
            $card = intval($data["card"]);
            $end = intval($data["end"]);
            if ($card<1000000000){
                return ['status' => 'n', 'info' => '开始卡号错误'];
            }
            if ($card>9999999999){
                return ['status' => 'n', 'info' => '开始卡号错误'];
            }
            if ($end<1000000000){
                return ['status' => 'n', 'info' => '结束卡号错误'];
            }
            if ($end>9999999999){
                return ['status' => 'n', 'info' => '结束卡号错误'];
            }
            $sqldata=[];
            $sqldata["agent2_time"]=time();
            $sqldata["agent2_id"]=$id;
            $res = Db::table("tb_card_info")->where("card",">=",$data["card"])->where("card","<=",$end)
                ->where("status",0)->where("agent_id",$obj["parent"])->where("agent2_id is null")->update($sqldata);

            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败:'.Db::table("tb_card_info")->getLastSql()];
            }
        }
        return view('', ['row' => $row,'data' => $obj]);
    }

    public function shopCard(){
        $id = $this->request->param('id');
        $obj = Db::table("tb_card_shop")->where("type",3)->where('id', $id)->find();
        $row = Db::table("tb_card_info")->where("status",0)->where("agent2_id",$obj["parent"])->where("shop_id is null")->order("id asc")->find();
        if ($this->request->isPost()) {
            if (!$obj){
                return ['status' => 'n', 'info' => '商户不存在'];
            }
            if (!$obj["bonus"]){
                return ['status' => 'n', 'info' => '请设置派卡分红比例'];
            }
            $data = $this->request->post();
            $card = intval($data["card"]);
            $end = intval($data["end"]);
            if ($card<1000000000){
                return ['status' => 'n', 'info' => '开始卡号错误'];
            }
            if ($card>9999999999){
                return ['status' => 'n', 'info' => '开始卡号错误'];
            }
            if ($end<1000000000){
                return ['status' => 'n', 'info' => '结束卡号错误'];
            }
            if ($end>9999999999){
                return ['status' => 'n', 'info' => '结束卡号错误'];
            }
            $sqldata=[];
            $sqldata["shop_time"]=time();
            $sqldata["shop_id"]=$id;
            $res = Db::table("tb_card_info")->where("card",">=",$data["card"])->where("card","<=",$end)
                ->where("status",0)->where("agent2_id",$obj["parent"])->where("shop_id is null")->update($sqldata);

            if ($res) {
                return ['status' => 'y', 'info' => '操作成功'];
            } else {
                return ['status' => 'n', 'info' => '操作失败:'.Db::table("tb_card_info")->getLastSql()];
            }
        }
        return view('', ['row' => $row,'data' => $obj]);
    }


    public function shop_apply_list(){
        $obj = Db::table("tb_shop_apply")->field("apply.*,agent.nickname as agent,industry.name as industry_name")->alias("apply")->order('status asc, time desc');
        $param = $this->request->get();
        if (isset($param['word']) && $param['word']) {
            $obj->where('apply.mobile', "like","%".$param['word']."%");
        }
        if (isset($param['start']) && $param['start']) {
            $obj->where('apply.time', '>', strtotime($param['start'] . " 00:00:00"));
        }
        if (isset($param['end']) && $param['end']) {
            $obj->where('apply.time', '<', strtotime($param['end'] . " 00:00:00"));
        }
        if (isset($param['status']) && $param['status']>-1) {
            $obj->where('apply.status', $param['status']);
        }
        if (isset($param['agent_id']) && $param['agent_id']>0) {
            $obj->where('apply.uid', $param['agent_id']);
        }

        $obj->join('tb_card_shop agent',"apply.uid=agent.id","left");
        $obj->join('tb_industry industry',"apply.industry=industry.id","left");
        $list = $obj->paginate(10, false, ['query' => $this->request->param()]);

        $agents =  Db::table("tb_card_shop")->where("type",1)->select();
        $industrys =  Db::table("tb_industry")->select();
        return view('', ['list' => $list,'agents' => $agents,"industrys"=>$industrys]);
    }

    public function shop_apply_detail(){
        $id = $this->request->param('id');

        $obj = Db::table("tb_shop_apply")->field("apply.*,agent.nickname as agent,industry.name as industry_name")->alias("apply")->order('status asc, time desc');
        $obj->join('tb_card_shop agent',"apply.uid=agent.id","left");
        $obj->join('tb_industry industry',"apply.industry=industry.id","left");
        $row = $obj->where("apply.id",$id)->find();
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data["sp_time"]=time();
            Db::startTrans();
            try{

                if ($data["status"]==1){
                    $sqldata = array();
                    $sqldata["username"]=$row["mobile"];
                    $sqldata["nickname"]=$row["name"];
                    $sqldata["parent"]=$row["uid"];
                    $sqldata["type"]=2;
                    $sqldata["time"]=time();
                    $sqldata["bonus"]=0;
                    $sqldata["type"]=$row["type"];
                    $sqldata["pass"]=md5($row["pass"]);
                    $insertId = Db::table("tb_card_shop")->insertGetId($sqldata);
                    if (!$insertId){
                        Db::rollback();
                        return ['status' => 'n', 'info' => '添加失败'];
                    }
                    $data["shop_id"]=$insertId;
                    $res =Db::table("tb_shop_apply")->where("id",$id)->update($data);
                    if (!$res){
                        Db::rollback();
                        return ['status' => 'n', 'info' => '操作失败'];
                    }
                }else if ($data["status"]==2){
                    $res =Db::table("tb_shop_apply")->where("id",$id)->update($data);
                    if (!$res){
                        Db::rollback();
                        return ['status' => 'n', 'info' => '操作失败'];
                    }
                }else{
                    Db::rollback();
                    return ['status' => 'n', 'info' => '审核状态不对'];
                }
                Db::commit();
                return ['status' => 'y', 'info' => '操作成功'];
            }catch (Exception $e){
                return ['status' => 'n', 'info' => '异常：'.$e->getMessage()];
            }

        }else{
            $license = get_table_field('tb_file','id',$row['license'],'save_name,file_name,type');
            $row["license_url"]=$license;
            $img = get_table_field('tb_file','id',$row['img'],'save_name,file_name,type');
            $row["img_url"]=$img;
        }
        return view('', ['data' => $row]);
    }


}